package cn.ycc1.baseset.basics;

import java.util.List;

/**
 * 数组
 * @author ycc
 * @date 2025/3/3
 * An array is a container object that holds a fixed number of values of a single type.
 * The length of an array is established when the array is created.
 *
 */
public class Arrays {
    /**
     * 1. Declaring a Variable to Refer to an Array
     */
    public void declaring() {
        // declares an array of integers
        int[] anArray;

        byte[] anArrayOfBytes;
        short[] anArrayOfShorts;
        long[] anArrayOfLongs;
        float[] anArrayOfFloats;
        double[] anArrayOfDoubles;
        boolean[] anArrayOfBooleans;
        char[] anArrayOfChars;
        String[] anArrayOfStrings;

    }

    /**
     * 2. Creating, Initializing, and Accessing an Array
     */
    public void create() {
        // create an array of integers
        int[] anArray = new int[10];
        anArray[0] = 100; // initialize first element
        anArray[1] = 200; // initialize second element
        anArray[2] = 300; // and so forth

        System.out.println("Element 1 at index 0: " + anArray[0]);
        System.out.println("Element 2 at index 1: " + anArray[1]);
        System.out.println("Element 3 at index 2: " + anArray[2]);

        int[] anArray2 = {
                100, 200, 300,
                400, 500, 600,
                700, 800, 900, 1000
        };
    }

    public static void multiDimArray() {
        String[][] names = {
                {"Mr. ", "Mrs. ", "Ms. "},
                {"Smith", "Jones"}
        };
        // Mr. Smith
        System.out.println(names[0][0] + names[1][0]);
        // Ms. Jones
        System.out.println(names[0][2] + names[1][1]);
    }

    public static void arraycopy() {
        String[] copyFrom = {
                "Affogato", "Americano", "Cappuccino", "Corretto", "Cortado",
                "Doppio", "Espresso", "Frappucino", "Freddo", "Lungo", "Macchiato",
                "Marocchino", "Ristretto" };

        String[] copyTo = new String[7];
        System.arraycopy(copyFrom, 2, copyTo, 0, 7);
        for (String coffee : copyTo) {
            System.out.print(coffee + " ");
        }

        java.util.Arrays.stream(copyTo).map(coffee -> coffee + " ").forEach(System.out::println);
        System.out.println(java.util.Arrays.toString(copyTo));
    }

    /**
     * Using the Var Type Identifier
     */
    public static void varType() {
        var list = List.of("one", "two", "three", "four");
        for (var element: list) {
            System.out.println(element);
        }
    }

    public static void main(String[] args) {
        // declares an array of integers
        int[] anArray;

        // allocates memory for 10 integers
        anArray = new int[10];

        // initialize first element
        anArray[0] = 100;
        // initialize second element
        anArray[1] = 200;
        // and so forth
        anArray[2] = 300;
        anArray[3] = 400;
        anArray[4] = 500;
        anArray[5] = 600;
        anArray[6] = 700;
        anArray[7] = 800;
        anArray[8] = 900;
        anArray[9] = 1000;

//        System.out.println("Element at index 0: "
//                + anArray[0]);
//        System.out.println("Element at index 1: "
//                + anArray[1]);
//        System.out.println("Element at index 2: "
//                + anArray[2]);
//        System.out.println("Element at index 3: "
//                + anArray[3]);
//        System.out.println("Element at index 4: "
//                + anArray[4]);
//        System.out.println("Element at index 5: "
//                + anArray[5]);
//        System.out.println("Element at index 6: "
//                + anArray[6]);
//        System.out.println("Element at index 7: "
//                + anArray[7]);
//        System.out.println("Element at index 8: "
//                + anArray[8]);
//        System.out.println("Element at index 9: "
//                + anArray[9]);
        multiDimArray();
        arraycopy();
    }
}
